<%@ Page language="vb" CodeFile="editInvoiceDurationPenalty.aspx.vb" Inherits="editInvoiceDurationPenalty_aspx_vb" %>

<%
    rowNumber = 1
    invoiceID = Request.QueryString("invoiceID")
    If Len(invoiceID) > 0 Then 
        invoiceID = CLng(invoiceID)
    End IF
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        dbOpen()
        RecSet = Conn.Execute("Select pi.invoiceAmount, pi.uw, pi.paymentTerm, pi.invoiceDate, pi.invoiceText, c.countryCurrency, c.country, p.partnerName, cl.client, pr.product, pl.priceListDuration, pl.priceListCategory, pl.price, pl.priceListDistance, (Select Count(ic.vehicleID) From (tblInvoiceContent ic INNER JOIN tblInvoice i ON i.invoiceID = ic.invoiceID) Where ic.vehicleID = v.vehicleID AND i.invoiceAmount > 0) As vehicleCount From ((((((tblInvoiceDurationPenalty pi INNER JOIN tblVehicle v ON v.vehicleID = pi.vehicleID) INNER JOIN tblCountry c ON c.countryID = v.countryID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblClient cl ON cl.clientID = v.clientID) INNER JOIN tblProduct pr ON pr.productID = v.productID) INNER JOIN tblPriceList pl ON pl.priceListID = v.priceListID) Where pi.invoiceDurationPenaltyID = " & invoiceID & "")
        If RecSet.EOF Then 
            Response.Redirect("searchVehicle.aspx?")
        Else
            invoiceAmount = RecSet.Fields("invoiceAmount").Value
            uw = RecSet.Fields("uw").Value
            paymentTerm = RecSet.Fields("paymentTerm").Value
            invoiceDate = RecSet.Fields("invoiceDate").Value
            invoiceText = RecSet.Fields("invoiceText").Value
            countryCurrency = RecSet.Fields("countryCurrency").Value
            country = RecSet.Fields("country").Value
            partnerName = RecSet.Fields("partnerName").Value
            client = RecSet.Fields("client").Value
            product = RecSet.Fields("product").Value
            priceListDuration = RecSet.Fields("priceListDuration").Value
            priceListCategory = RecSet.Fields("priceListCategory").Value
            price = RecSet.Fields("price").Value
            priceListDistance = RecSet.Fields("priceListDistance").Value
            vehicleCount = RecSet.Fields("vehicleCount").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If Len(invoiceText) > 0 Then 
            invoiceText = Trim(invoiceText)
            invoiceText = Replace(CStr(invoiceText), "&quot;", """")
            invoiceText = Replace(CStr(invoiceText), "&#39;", "'")
            invoiceText = Replace(CStr(invoiceText), "<br>", vbCrLf)
        End IF
        Over()
        strJS = ""
        strJS = strJS + "[""invoiceAmount"", ""==''""],"
        strJS = strJS + "[""uw"", ""==''""],"
        strJS = strJS + "[""paymentTerm"", ""==''""],"
        strJS = strJS + "[""invoiceDateYear"", ""<1""],"
        strJS = strJS + "[""invoiceDateMonth"", ""<1""],"
        strJS = strJS + "[""invoiceDateDay"", ""<1""]"
        checkFields()
        realTime = Now()
        year = Left(realTime, 4)
        month = Mid(realTime, 6, 2)
        day = Mid(realTime, 9, 2)
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>EDIT PENALTY INVOICE ID " & invoiceID & "<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='editInvoiceDurationPenalty.aspx?do=save&invoiceID=" & invoiceID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr><td class='formBold'>Invoice Amount (" & countryCurrency & "):</td><td align='right'><input type='text' name='invoiceAmount' id='invoiceAmount' value='" & invoiceAmount & "' size='40' maxlength='50' onkeyup='re=/([\d\,]*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_invoiceAmount' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>UW (" & countryCurrency & "):</td><td align='right'><input type='text' name='uw' id='uw' value='" & uw & "' size='40' maxlength='50' onkeyup='re=/([\d\,]*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_uw' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Payment term (days):</td><td align='right'><input type='text' name='paymentTerm' id='paymentTerm' value='" & paymentTerm & "' size='40' maxlength='50' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_paymentTerm' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Invoice date:&nbsp; </td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='invoiceDateYear' id='invoiceDateYear'>")
        Response.Write("<option value=''>&nbsp; Year ")
        stopYear = CDbl(year) + 3
        yearCounter = year
        For yearLoop = year To stopYear
            Response.Write("<option value='" & yearCounter & "'")
            If CStr(yearCounter) = Left(invoiceDate, 4) Then 
                Response.Write("selected='selected'")
            End IF
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write(" - ")
        Response.Write("<select name='invoiceDateMonth' id='invoiceDateMonth'>")
        Response.Write("<option value=''>&nbsp; Month ")
        yearCounter = 1
        For yearLoop = 1 To 12
            Response.Write("<option value='" & yearCounter & "'")
            If CLng(yearCounter) = CLng(Mid(invoiceDate, 6, 2)) Then 
                Response.Write("selected='selected'")
            End IF
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write(" - ")
        Response.Write("<select name='invoiceDateDay' id='invoiceDateDay'>")
        Response.Write("<option value=''>&nbsp; Day&nbsp; ")
        yearCounter = 1
        For yearLoop = 1 To 31
            Response.Write("<option value='" & yearCounter & "'")
            If CLng(yearCounter) = CLng(Right(invoiceDate, 2)) Then 
                Response.Write("selected='selected'")
            End IF
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td>")
        Response.Write("<div id='error_invoiceDateYear' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_invoiceDateMonth' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_invoiceDateDay' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td class='formBold'>Invoice text:&nbsp; </td></tr>")
        Response.Write("<tr><td colspan='2' align='right'><textarea name='invoiceText' id='invoiceText' style='width: 100%; height: 50px'>" & invoiceText & "</textarea></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td align='right' colspan='3'>")
        Response.Write("<input type='submit' value='Save changes'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='4'>LEASE INFORMATION<hr class='hrHeadline'></td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Country:</td><td>" & country & "</td></tr>")
        Response.Write("<tr><td>Partner:</td><td>" & partnerName & "</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Client:</td><td>" & client & "</td></tr>")
        Response.Write("<tr><td>Product:</td><td>" & product & "</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Duration:</td><td>" & vehicleCount & "/" & priceListDuration & " months</td></tr>")
        Response.Write("<tr><td>Driving distance:</td><td>" & priceListDistance & " km</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Price category:</td><td>" & priceListCategory & " - " & price & " " & countryCurrency & "</td></tr>")
        Response.Write("<tr><td colspan='4'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
        invoiceAmount = Request.Form("invoiceAmount")
        uw = Request.Form("uw")
        paymentTerm = Request.Form("paymentTerm")
        invoiceDateYear = Request.Form("invoiceDateYear")
        invoiceDateMonth = Request.Form("invoiceDateMonth")
        invoiceDateDay = Request.Form("invoiceDateDay")
        invoiceText = Request.Form("invoiceText")
        invoiceAmount = CDbl(invoiceAmount)
        invoiceAmount = Replace(CStr(invoiceAmount), ",", ".")
        uw = CDbl(uw)
        uw = Replace(CStr(uw), ",", ".")
        paymentTerm = CLng(paymentTerm)
        invoiceDateYear = CLng(invoiceDateYear)
        invoiceDateMonth = CLng(invoiceDateMonth)
        invoiceDateDay = CLng(invoiceDateDay)
        If CLng(invoiceDateMonth) < 10 Then 
            invoiceDateMonth = "0" & invoiceDateMonth & ""
        End IF
        If CLng(invoiceDateDay) < 10 Then 
            invoiceDateDay = "0" & invoiceDateDay & ""
        End IF
        invoiceDate = invoiceDateYear & "-" & invoiceDateMonth & "-" & invoiceDateDay
        expiryDate = DateAdd("d", CDbl(paymentTerm), Convert.ToDateTime(invoiceDate))
        invoiceClaim = invoiceAmount
        If Len(invoiceText) > 0 Then 
            invoiceText = Trim(invoiceText)
            invoiceText = Replace(CStr(invoiceText), """", "&quot;")
            invoiceText = Replace(CStr(invoiceText), "'", "&#39;")
            invoiceText = Replace(CStr(invoiceText), vbCrLf, "<br>")
        End IF
        Response.Write("vehicleID = " & vehicleID & "<br>")
        Response.Write("invoiceAmount = " & invoiceAmount & "<br>")
        Response.Write("invoiceClaim = " & invoiceClaim & "<br>")
        Response.Write("uw = " & uw & "<br>")
        Response.Write("invoiceDate = " & invoiceDate & "<br>")
        Response.Write("expiryDate = " & expiryDate & "<br>")
        Response.Write("paymentTerm = " & paymentTerm & "<br>")
        Response.Write("invoiceText = " & invoiceText & "<br>")
        dbOpen()
        Conn.Execute("Update tblInvoiceDurationPenalty SET invoiceAmount = " & invoiceAmount & ", invoiceClaim = " & invoiceClaim & ", uw = " & uw & ", invoiceDate = '" & invoiceDate & "', expiryDate = '" & expiryDate & "', paymentTerm = " & paymentTerm & ", invoiceText = '" & invoiceText & "' Where invoiceDurationPenaltyID = " & invoiceID & "")
        dbClose()
        Response.Redirect("editInvoiceDurationPenalty.aspx?invoiceID=" & invoiceID & "&subDo=%20-%20UPDATED")
    End IF
%>
